#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#include <string.h>


// ./demo9 restart|stop
int main(int argc, char const *argv[])
{
    
    key_t k = ftok("/", 1); // 获取IPC的键值
    int shmid = shmget(k, 4, IPC_CREAT|0644);
    if(shmid == -1){
        perror("shmget");
        return -1;
    }
    int *cmd = (int *)shmat(shmid, NULL, 0);
    if(cmd == NULL){
        perror("shmat");
        return -1;
    }

    if(strncmp("restart", argv[1], 7) == 0){
        *cmd = 1;
    }else if(strncmp("stop", argv[1], 4) == 0){
        *cmd = 2;
    }else{
        *cmd = 3;
    }

    printf("OK\n");
    shmdt(cmd);  // 取消映射

    return 0;
}
